using System;
using System.Data;
using System.Reflection;
using System.Collections;
using System.Collections.Specialized;
using System.Xml.XPath;

using Actions;

namespace Actions.Databases
{
	/// <summary>
	/// DatabasePool control multiplied data sources
	/// </summary>
	public class DatabasesPool
	{
		private ListDictionary		_p;
		
		public string info 
		{
			get 
			{
				string s = "<databasespool>";
			
				foreach(DictionaryEntry db in _p) {
					s += ((Database) db.Value).info;
				}				
				return s + "</databasespool>";	
			}
		}	
		
		public ListDictionary p
		{
			get
			{
				return _p;
			}
		}
			
		public DatabasesPool(ref Document d)
		{
			XPathNodeIterator i = d.Select("//Configuration/DatabasesPool/PoolItem");
			
			_p = new ListDictionary();
			while(i.MoveNext()) {
				_p.Add(i.Current.GetAttribute("id", ""), new Database(new Document(i.Current)));
			}
		}
		
		public void connect()
		{
			foreach(DictionaryEntry db in _p) {
				((Database) db.Value).connectOnStart();
			}
		}
		
		public void disconnect()
		{
			foreach(DictionaryEntry db in _p) {
				((Database) db.Value).disconnect();
			}
		}		
	}
}
